<template>
  <div class="nav-menu">
    <div class="logo">
      <img src="~@/assets/img/logo.svg" alt="logo" class="img" />
      <span v-if="!collapse" class="title">Vue3+Ts+Vite</span>
    </div>
    <el-menu
      class="el-menu-vertical"
      :default-active="defaultValue"
      text-color="#fff"
      active-text-color="#0a60bd"
      background-color="#001529"
      :collapse="collapse"
    >
      <!-- 渲染菜单栏 -->
      <template v-for="(item, index) in userMenus" :key="item.id + ''">
        <!-- 判断菜单栏项的类型 type = 1 可以下拉的一级菜单 -->
        <template v-if="item.type === 1">
          <!-- 动态添加唯一标识 index 字符串类型-->
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <el-icon v-if="item.icon">
                <!-- 动态组件 图标-->
                <component :is="icons[index]"></component>
              </el-icon>
              <span>{{ item.name }}</span>
            </template>
            <!-- 展开后的 菜单项 type = 2 -->
            <template v-for="subItem in item.children" :key="subItem.id">
              <el-menu-item
                :index="subItem.id + ''"
                @click="handleMenuItemClick(subItem)"
              >
                <span>{{ subItem.name }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <!-- 普通一级菜单不可以下拉 -->
        <template v-else-if="item.type === 2">
          <el-menu-item :index="item.id + ''">
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import { useLoginStore } from '@/store/modules/login'
import { useRouter, useRoute } from 'vue-router'
import { pathMapToMenu } from '@/utils/mapMenus'

defineProps({
  collapse: {
    type: Boolean,
    default: false
  }
})
// router
const router = useRouter()

// data
const currentPath = useRoute().path // 根据路由的路径来匹配到默认选择的菜单
const userMenus = useLoginStore().userMenus
const icons = ref(['Monitor', 'Setting', 'ShoppingBag', 'ChatDotRound'])
const menu = pathMapToMenu(useLoginStore().userMenus, currentPath)
const defaultValue = ref(menu.id + '')

// 路由跳转 只有展开的二级菜单才有响应的界面
const handleMenuItemClick = (item: any) => {
  router.push({
    path: item.url ?? '/not-found'
  })
}
</script>

<style scoped lang="less">
.nav-menu {
  height: 100%;
  background-color: #001529;

  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;

    .img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
  }
  .el-menu {
    border-right: none;
  }

  // 目录
  .el-sub-menu {
    background-color: #001529 !important;
    // 二级菜单 ( 默认背景 )
    .el-menu-item {
      padding-left: 50px !important;
      background: #16293b !important;
    }
  }
  // ::v-deep 的新的写法
  :deep(.el-submenu__title) {
    background-color: #001529 !important;
  }

  // hover 高亮
  .el-menu-item:hover {
    color: #fff !important; // 菜单
  }

  .el-menu-item.is-active {
    color: #fff !important;
    background-color: #0a60bd !important;
  }
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  background: #16293b !important;
  height: calc(100% - 48px);
}
</style>
